import uvicorn
from fastapi import FastAPI
from starlette.staticfiles import StaticFiles

from tutorial import app03, app04, app05, app06, app07

# from fastapi.exceptions import RequestValidationError
# from fastapi.responses import PlainTextResponse
# from starlette.exceptions import HTTPException as StarletteHTTPException

app = FastAPI(
    title='FastAPI Tutorial',
    description='FastAPI 教程',
    version='1.0.0',
    docs_url='/docs',
    redoc_url='/redocs',
    # dependencies=[Depends(verify_token), Depends(verify_key)] # 全局依赖
)

# mount表示将某个目录下一个完全独立的应用挂载过来，这个不会在API交互文档中显示
app.mount(path='/static', app=StaticFiles(directory='./static'), name='static')

# @app.exception_handler(StarletteHTTPException)  # 重写HTTPException异常处理器
# async def http_exception_handler(request, exc):
#     """
#         :param request: 这个参数不能省
#         :param exc:
#         :return:
#     """
#     return PlainTextResponse(str(exc.detail), status_code=exc.status_code)


# @app.exception_handler(RequestValidationError)  # 重写请求验证异常处理器
# async def validation_exception_handler(request, exc):
#     """
#         :param request: 这个参数不能省
#         :param exc:
#         :return:
#     """
#     print("调用了")
#     return PlainTextResponse(str(exc), status_code=400)


app.include_router(app03, prefix='/chapter03', tags=['第三章 请求参数和验证'])
app.include_router(app04, prefix='/chapter04', tags=['第四章 响应处理和FastAPI配置'])
app.include_router(app05, prefix='/chapter05', tags=['第五章 FastAPI的依赖注入系统'])
app.include_router(app06, prefix='/chapter06', tags=['第六章 安全认证和授权'])
app.include_router(app07, prefix='/chapter07', tags=['第六章 安全认证和授权'])

if __name__ == "__main__":
    uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True, workers=1)
